//
//  UserViewModel.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/11/27.
//  Copyright © 2017年 beisheng. All rights reserved.
//OneGoodsModel

#import "UserViewModel.h"
#import "LoginViewController.h"
#import "BaseNavigationController.h"

#import <JPUSHService.h>
@interface UserViewModel ()

@end

@implementation UserViewModel

//implimentationSingleton(UserViewModel)

static UserViewModel *_instance;
+(instancetype)shareUserViewModel{
    
    if (!_instance) {
        
        UserViewModel *instance = [[self alloc]init];
        
        _instance = instance;
    }
    return _instance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}
- (id)copyWithZone:(nullable NSZone *)zone{
    return _instance;
}
-(id)mutableCopyWithZone:(NSZone *)zone{
    return _instance;
}

- (instancetype)init{
    
    if (self = [super init]) {
        
        
        UserInfo *user = [NSKeyedUnarchiver unarchiveObjectWithFile:[@"accout.plist" cachesDir]];
    
        if (user) {
            
            self.user = user;
        }
       
        
        
        ShopCar *tmpShopCar = [NSKeyedUnarchiver unarchiveObjectWithFile:[@"shopCar.plist" cachesDir]];
        
        if (tmpShopCar) {
            
            self.shopCar = tmpShopCar;
            
        }else{
            
            self.shopCar = [[ShopCar alloc]init];
            
            self.shopCar.shopCarList = [NSMutableArray array];
        }
        
        
        SearchHistoryModel *history = [NSKeyedUnarchiver unarchiveObjectWithFile:[@"history.plist" cachesDir]];
        
        if (history) {
            
            self.searchHistory = history;
            
        } else {
            
            self.searchHistory = [[SearchHistoryModel alloc]init];
            
            self.searchHistory.searchHistoryStrs = [NSMutableArray array];
        }
            

    }
    
    
    
    return self;
    
}




- (NSString *)uid{
    
     return self.user.uid;
    
}

- (NSString *)nick_name{
    
    return self.user.nick_name;
}



- (BOOL)userIsLogin{
    
    return self.user != nil;
}

- (void)setShopCarIdetLabel:(UILabel *)shopCarIdetLabel{
    _shopCarIdetLabel = shopCarIdetLabel;
    
    if (self.badgeValue.integerValue > 0) {
        
        self.shopCarIdetLabel.hidden = NO;

    } else {
        
        self.shopCarIdetLabel.hidden = YES;

    }
    
    
    
}

- (void)setBadgeValue:(NSString *)badgeValue{
    
    _badgeValue = badgeValue;
    
    if (self.shopCarIdetLabel != nil) {
        if (_badgeValue.integerValue > 0) {
            self.shopCarIdetLabel.hidden = NO;
        }else{
            
            self.shopCarIdetLabel.hidden = YES;

        }
    }
    
    
    
//      [UIApplication sharedApplication].keyWindow.rootViewController.childViewControllers[1].tabBarItem.badgeValue = _badgeValue;
}




-(void)logOut{
    
    
    [UserViewModel shareUserViewModel].user = nil;
    
    [[UserViewModel shareUserViewModel].shopCar.shopCarList removeAllObjects];
    
    [[UserViewModel shareUserViewModel].shopCar saveUserInfo];
    
    
    [[UserViewModel shareUserViewModel].searchHistory.searchHistoryStrs removeAllObjects];

    [[UserViewModel shareUserViewModel].searchHistory saveHistoryInfo];
    
    NSString *documentsPath = [@"accout.plist" cachesDir];

    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    
    BOOL res = [fileManager removeItemAtPath:documentsPath error:nil];
    
    if (res) {
        
        [JPUSHService deleteTags:[NSSet setWithObject:@"feifubao"] completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
            
            NSLog(@"%ld-------",iResCode);
            
        } seq:myViewModel.user.user_tel.integerValue];
        
        
        [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            
            NSLog(@"%ld-------",iResCode);
            
        } seq:myViewModel.user.user_tel.integerValue + 1];
        
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"userDidLoginOrInfoChange" object:nil];
        
    
        
       

//        CATransition *tranition = [CATransition animation];//转场动画
//
//        tranition.type = @"reveal";//动画类型reveal
//
//        tranition.duration = 0.5;//动画时长
//
//        [[UIApplication sharedApplication].keyWindow.layer addAnimation:tranition forKey:nil];
//        //
//
//
//        [UIApplication sharedApplication].keyWindow.rootViewController = [[BaseNavigationController alloc]initWithRootViewController:[[LoginViewController alloc]init]];
        
        
    }else{
        
        
        NSLog(@"文件删除失败");
        
        NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:documentsPath]?@"YES":@"NO");
        
        
    }
    
}

@end
